package tree.recurrence;

import tree.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName PreOrder
 * @Description TODO
 * @Author lenovo
 * @Date 2023-03-07 10:53
 * @Version 1.0
 * @Comment Magic. Do not touch.
 * If this comment is removed. the program will blow up
 */
public class PreOrder {

    public List<Integer> preOrderTraversal(TreeNode root) {
        List<Integer> result = new ArrayList<>();
        preOrder(root, result);
        return result;
    }


    public void preOrder(TreeNode root, List<Integer> res) {

        if (root == null) {
            return;
        }

        res.add(root.val);

        preOrder(root.left, res);

        preOrder(root.right, res);
    }


    public static void main(String[] args) {
        TreeNode treeNode = new TreeNode(3);

        TreeNode treeNode1 = new TreeNode(1);
        TreeNode treeNode2 = new TreeNode(2);

        treeNode.left = treeNode1;
        treeNode.right = treeNode2;

        PreOrder preOrder = new PreOrder();
        List<Integer> list = preOrder.preOrderTraversal(treeNode);
        for (Integer integer : list) {
            System.out.println(integer);
        }
    }
}